package com.woniuxy.rms.service.impl;
import com.woniuxy.rms.dao.MenusMapper;
import com.woniuxy.rms.dao.RoleMenuMapper;
import com.woniuxy.rms.dto.MenusDTO;
import com.woniuxy.rms.pojo.Admins;
import com.woniuxy.rms.pojo.Menus;
import com.woniuxy.rms.service.MenuService;
import com.woniuxy.rms.util.DataResult;
import com.woniuxy.rms.util.SystemConstant;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.security.auth.Subject;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
@Transactional(rollbackFor = Exception.class)
public class MenuServiceImpl implements MenuService {

    @Autowired
    MenusMapper menusMapper;

    @Autowired
    RoleMenuMapper roleMenuMapper;


    @Override
    public List<MenusDTO> getMenusByRoleId() {
        //获取用户信息
        Admins admin = (Admins) SecurityUtils.getSubject().getPrincipal();
        //通过用户的角色获取菜单数据
        List<Menus> menus = menusMapper.findByRoleId(admin.getRoleId());
        //声明返回结果
        List<MenusDTO> result = new ArrayList<>();
        for(Menus menu : menus){
            if (menu.getParentId().equals(SystemConstant.MENU_ROOT_ID)){//获取根节点
                //封装父节点对象
                MenusDTO dto = new MenusDTO();
                dto.setTitle(menu.getTitle());
                dto.setHref(menu.getHref());
                dto.setIcon(menu.getIcon());
                dto.setSpread(menu.getSpread());
                List<MenusDTO> childs = new ArrayList<>();
                for (Menus childMenu : menus){//查找当前节点下的子节点
                    if (childMenu.getParentId().equals(menu.getId())){
                        //封装子节点对象
                        MenusDTO child = new MenusDTO();
                        child.setTitle(childMenu.getTitle());
                        child.setHref(childMenu.getHref());
                        child.setIcon(childMenu.getIcon());
                        child.setSpread(childMenu.getSpread());
                        childs.add(child);
                    }
                }
                dto.setChildren(childs);
                result.add(dto);
            }
        }
        return result;
    }

    @Override
    public List<Menus> findAll() {
        return menusMapper.findAll();
    }

    @Override
    public DataResult saveMenu(Menus menus) {
        //判断同级别下的菜单名是否重复
        List<Menus> menuList = menusMapper.findByTitle(menus.getTitle());
        for (Menus menu : menuList) {
            if (menu.getParentId().equals(menus.getParentId())){
                return DataResult.failed("菜单名"+"'"+menus.getTitle()+"'"+"已存在！");
            }
        }
        //获取创建人
        Admins admins = (Admins) SecurityUtils.getSubject().getPrincipal();
        menus.setAdminId(admins.getId());
        menus.setCreateTime(new Date());
        //添加菜单数据
        menusMapper.insertSelective(menus);
        return DataResult.success();
    }

    @Override
    public void deleteMenu(Integer id) {
        //删除中间表数据
        roleMenuMapper.deleteByMenuId(id);
        //删除菜单数据
        menusMapper.deleteById(id);
    }

    @Override
    public DataResult updateMenu(Menus menus) {
        //判断同级别下的菜单名是否重复
        List<Menus> menuList = menusMapper.findByTitle(menus.getTitle());
        for (Menus menu : menuList) {
            if (menu.getParentId().equals(menus.getParentId())){
                //排除自己
                if (!menu.getId().equals(menus.getId())){
                    return DataResult.failed("菜单名"+"'"+menus.getTitle()+"'"+"已存在！");
                }
            }
        }
        menus.setUpdateTime(new Date());
        menusMapper.updateByPrimaryKeySelective(menus);
        return DataResult.success();
    }

    @Override
    public Menus findById(Integer menuId) {
        return menusMapper.selectByPrimaryKey(menuId);
    }

    @Override
    public List<Menus> findCheckMenusByRoles(Integer roleId) {
        //查询所有菜单
        List<Menus> menus = menusMapper.findAll();
        //查询该角色的菜单
        List<Menus> currMenus = menusMapper.findCheckMenusByRoles(roleId);
        //改变该角色的菜单状态
        for (Menus menu : menus) {
            for (Menus currMenu : currMenus) {
                if (menu.getId().equals(currMenu.getId())){
                    menu.setChecked("true");
                }
            }
        }
        return menus;
    }
}